package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyHengRuo;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  安心分期-恒诺API无锡
 *
 *  @author 吴佳宝
 */
@ApiPlan(productId = "46", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5HengRuo implements ApiOrgPhoneMd5ConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("data", apiDataExchangeParameter.getPhone());
        });
        return map;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        apiApplyParameter.setExtendUrl("?code=WxbvewlQbam9ZoxyGrT4");
        OrgApplyHengRuo data = new OrgApplyHengRuo();
        //  手机号
        data.setPhone(apiApplyParameter.getPhone());
        //  城市
        data.setCity(apiApplyParameter.getCity());
        //  姓名
        data.setName(apiApplyParameter.getUserName());

        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  年龄
        data.setAge(apiFieldConverter.getAge());
        //  贷款额度
        data.setMoney(apiFieldConverter.getLoanAmountInt());
        data.setRemark(apiFieldConverter.getExtendInfo());
        data.setSex(apiFieldConverter.getSexText());
        data.setSource("恒诺");
        String json = JsonUtils.toJson(data);
        return json;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
